<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      class="formSearch"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-button
          @click="getDataList()"
          size="mini"
          v-if="isAuth('cms:tbAlarmMonitor:list')"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      height="80vh"
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column
        prop="flowMeterName"
        header-align="center"
        align="center"
        label="地点"
      />
      <el-table-column
        prop="flowTime"
        header-align="center"
        align="center"
        width="150"
        label="更新时间"
      />
      <el-table-column
        prop="flowValue"
        header-align="center"
        align="center"
        label="流量值"
      >
        <template slot-scope="scope">
          <span
            style="color:#f00;"
            v-if="scope.row.flowValue > scope.row.flowUpperLimit"
            >{{ scope.row.flowValue }}</span
          >
          <span
            style="color:#17B3A3;"
            v-else-if="scope.row.flowValue < scope.row.flowLowerLimit"
            >{{ scope.row.flowValue }}</span
          >
          <span v-else>{{ scope.row.flowValue }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="flowTime"
        header-align="center"
        align="center"
        label="流量更新时间"
      />
      <el-table-column
        prop="pressValue"
        header-align="center"
        align="center"
        label="压力值"
      >
        <template slot-scope="scope">
          <span
            style="color:#f00;"
            v-if="scope.row.pressValue > scope.row.pressUpperLimit"
            >{{ scope.row.pressValue }}</span
          >
          <span
            style="color:#17B3A3;"
            v-else-if="scope.row.pressValue < scope.row.pressLowerLimit"
            >{{ scope.row.pressValue }}</span
          >
          <span v-else>{{ scope.row.pressValue }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="pressTime"
        header-align="center"
        align="center"
        label="压力更新时间"
      />
      <el-table-column header-align="center" align="center" label="通电状况">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1" style="color: #00ff00;">市电</span>
          <span
            style="color: #f00;font-weight: 600;"
            v-else-if="scope.row.status == 0"
            >蓄电池</span
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="electricTime"
        header-align="center"
        align="center"
        label="通电更新时间"
      />
      <el-table-column
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            v-if="
              isAuth('cms:tbAlarmRecord:save') &&
                (scope.row.alarmMonitorId === '' ||
                  scope.row.alarmMonitorId === null)
            "
            @click="addOrUpdateHandle(scope.row.equipmentId)"
            >阀值设置
          </el-button>
          <el-button
            type="text"
            size="small"
            v-else
            @click="
              addOrUpdateHandle(scope.row.equipmentId, scope.row.alarmMonitorId)
            "
            >阀值设置
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    />
  </div>
</template>

<script>
import AddOrUpdate from "./monitor-add-or-update";
import { isAuth } from "../../../utils";
export default {
  data() {
    return {
      dataForm: {
        key: null
      },
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      if (isAuth("cms:tbAlarmMonitor:list")) {
        this.$http({
          url: this.$http.adornUrl("/cms/tbAlarmMonitor/monitor"),
          method: "get"
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.vo;
          } else {
            this.dataList = [];
          }
          this.dataListLoading = false;
        });
      } else {
        this.$message.error("该用户没有权限浏览，请联系管理员添加权限");
        this.dataListLoading = false;
      }
    },
    // 新增阀值 / 修改阀值
    addOrUpdateHandle(equipmentId, id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(equipmentId, id);
      });
    }
  }
};
</script>
